package com.yserp.project.erp.basic.reservoir.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yserp.common.constant.Constants;
import com.yserp.common.utils.mybatis.MybatisPlusUtil;
import com.yserp.common.utils.text.Convert;
import com.yserp.project.erp.basic.reservoir.domain.BasisWarehouseReservoir;
import com.yserp.project.erp.basic.reservoir.mapper.BasisWarehouseReservoirMapper;
import com.yserp.project.erp.basic.reservoir.service.IBasisWarehouseReservoirService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 库区设置Service业务层处理
 *
 * @author yserp
 * @date 2022-07-06
 */
@Service
public class BasisWarehouseReservoirServiceImpl extends ServiceImpl<BasisWarehouseReservoirMapper,BasisWarehouseReservoir> implements IBasisWarehouseReservoirService {
    @Resource
    private BasisWarehouseReservoirMapper basisWarehouseReservoirMapper;

    /**
     * 查询库区设置
     *
     * @param id 库区设置主键
     * @return 库区设置
     */
    @Override
    public BasisWarehouseReservoir selectBasisWarehouseReservoirById(Long id) {
        return basisWarehouseReservoirMapper.selectById(id);
    }

    /**
     * 查询库区设置列表
     *
     * @param basisWarehouseReservoir 库区设置
     * @return 库区设置
     */
    @Override
    public List<BasisWarehouseReservoir> selectBasisWarehouseReservoirList(BasisWarehouseReservoir basisWarehouseReservoir) {
        QueryWrapper<BasisWarehouseReservoir> queryWrapper = new QueryWrapper<>();
        MybatisPlusUtil.notNullField(basisWarehouseReservoir,queryWrapper);
        return basisWarehouseReservoirMapper.selectList(queryWrapper);
    }

    /**
     * 新增库区设置
     *
     * @param basisWarehouseReservoir 库区设置
     * @return 结果
     */
    @Override
    public int insertBasisWarehouseReservoir(BasisWarehouseReservoir basisWarehouseReservoir) {
        return basisWarehouseReservoirMapper.insert(basisWarehouseReservoir);
    }

    /**
     * 修改库区设置
     *
     * @param basisWarehouseReservoir 库区设置
     * @return 结果
     */
    @Override
    public int updateBasisWarehouseReservoir(BasisWarehouseReservoir basisWarehouseReservoir) {
        return basisWarehouseReservoirMapper.updateById(basisWarehouseReservoir);
    }

    /**
     * 批量删除库区设置
     *
     * @param ids 需要删除的库区设置主键
     * @return 结果
     */
    @Override
    public int deleteBasisWarehouseReservoirByIds(String ids) {
        return basisWarehouseReservoirMapper.deleteBatchIds(Arrays.asList(Convert.toLongArray(ids)));
    }

    /**
     * 删除库区设置信息
     *
     * @param id 库区设置主键
     * @return 结果
     */
    @Override
    public int deleteBasisWarehouseReservoirById(Long id) {
        return basisWarehouseReservoirMapper.deleteById(id);
    }

    @Override
    public String checkReservoirCodeUnique(String reservoirCode) {
        String flag = Constants.NAME_UNIQUE;
        int count = basisWarehouseReservoirMapper.selectCount(new QueryWrapper<BasisWarehouseReservoir>().eq("reservoir_code",reservoirCode));
        if (count > 0) {
            flag = Constants.NAME_NOT_UNIQUE;
        }
        return flag;
    }
}
